home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyo (Python 2.5)
-
- import atexit
- import logging
- import httplib
- import util
- _log = log = logging.getLogger('asynchttp')
- import connection
- import httptypes
- __all__ = [
- 'HttpMaster',
- 'httpopen']
-
- class HttpMaster(object):
-
- def key(thing):
- if isinstance(thing, (connection.HttpPersister, connection.AsyncHttpConnection)):
- return '%s:%s' % (thing.host, thing.port)
- elif isinstance(thing, httptypes.HTTPRequest):
- host = thing.get_host()
- o_r_host = thing.get_origin_req_host()
- if o_r_host in host:
- if o_r_host != host:
- pass
- else:
- host = o_r_host
-
- url = thing.get_full_url()
- if ':' in host:
- (host, port) = host.split(':')
- elif url.startswith('https'):
- port = httplib.HTTPS_PORT
- else:
- port = httplib.HTTP_PORT
- return '%s:%s' % (host, port)
- else:
- raise TypeError("Don't know how to key this object: %r", thing)
-
- key = staticmethod(key)
-
- def __init__(self):
- self._conns = { }
-
-
- def __repr__(self):
- return '<%s with %r active connections (id=0x%x)>' % (type(self).__name__, len(self._conns), id(self))
-
-
- def request(self, full_url, data = None, *a, **k):
- cb = k.pop('callback')
- req = httptypes.HTTPRequest.make_request(full_url, data, *a, **k)
- self._do_request(req, cb)
-
- request = util.callsback(request)
-
- def _do_request(self, req, cb = None):
- if cb is None:
- cb = req.callback
-
- conn = self.get_connection(req)
- conn.request(req, callback = cb)
-
-
- def get_connection(self, forwhat):
- key = self.key(forwhat)
-
- try:
- conn = self._conns[key]
- except KeyError:
- conn = self._conns[key] = self._make_connection(key)
-
- return conn
-
-
- def _make_connection(self, key):
- log.info('Making new %r for key=%r', connection.HttpPersister, key)
- (host, s_port) = key.split(':')
- port = int(s_port)
- conn = connection.HttpPersister((host, port))
- self.bind_events(conn)
- return conn
-
-
- def bind_events(self, conn):
- bind = conn.bind_event
- bind('on_fail', self._failed_connection)
- bind('redirect', self._handle_redirect)
-
-
- def unbind_events(self, conn):
- unbind = conn.unbind
- unbind('on_fail', self._failed_connection)
- unbind('redirect', self._handle_redirect)
-
-
- def _handle_redirect(self, req):
- redirector = getattr(req, 'on_redirect', None)
- if redirector is not None:
- newreq = redirector(req)
- if newreq is None:
- req.callback.error('redirect cancelled')
- return None
-
- req = newreq
-
- self._do_request(req)
-
-
- def _failed_connection(self, conn):
- log.info('Removing failed connection: conn = %r, key(conn) = %r', conn, self.key(conn))
- self._conns.pop(self.key(conn))
- self.unbind_events(conn)
-
-
- def close_all(self):
- while self._conns:
- (_key, conn) = self._conns.popitem()
- self.unbind_events(conn)
- conn.close()
-
-
- _httpmaster = HttpMaster()
- atexit.register(_httpmaster.close_all)
-
- def httpopen(*a, **k):
- cb = k.pop('callback')
- _httpmaster.request(callback = cb, *a, **k)
-
- httpopen = util.callsback(httpopen)
-
- def main():
-
- def success(*a):
- print 'success', a
-
-
- def error(*a):
- print 'error', a
-
- httpopen('http://65.54.239.211/index.html', success = success, error = error)
-
- if __name__ == '__main__':
- from tests.testapp import testapp
- a = testapp()
- main()
- a.MainLoop()
-
-